package wangm.repository;

import wangm.bean.Notice;
import wangm.bean.NoticeQueryParameter;
import wangm.bean.Pagination;
import wangm.util.DBOperatorComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 通知repository
 *
 * @author zk
 * @since 2018/5/8
 */
@Repository
public class NoticeRepository {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(NoticeRepository.class);
    /**
     * db操作
     */
    @Autowired
    private DBOperatorComponent dbOperatorComponent;
    /**
     * 当前表名
     */
    public static final String thisTableName = "t_notice";

    /**
     * 添加notice
     *
     * @param notice notice
     */
    public void add(Notice notice){
        String sql = "insert into " + thisTableName + " (id,title,abstractContent,type,content,authorNumber,authorName,createTime) values(?,?,?,?,?,?,?,?)";
        try {
            dbOperatorComponent.update(sql,notice.getId(),notice.getTitle(),notice.getAbstractContent(),notice.getType(),notice.getContent(),
                    notice.getAuthorNumber(),notice.getAuthorName(),notice.getCreateTime());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    /**
     * 删除指定的通知
     *
     * @param id id
     */
    public void remove(String id){
        String sql = "delete from " + thisTableName + " where id = ?";
        try {
            dbOperatorComponent.update(sql,id);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    /**
     * 按照关键词分页查找
     *
     * @param queryParameter queryParameter
     * @param pagination pagination
     */
    public List<Notice> getList(NoticeQueryParameter queryParameter, Pagination<Notice> pagination){
        List parameter = new ArrayList();
        StringBuffer sqlBuffer = new StringBuffer("select * from " + thisTableName + " where 1 = 1 ");

        if(!StringUtils.isEmpty(queryParameter.getKeyWords())){
            sqlBuffer.append(" and title like ? ");
            parameter.add("%" + queryParameter.getKeyWords() + "% ");
        }

        if(!StringUtils.isEmpty(queryParameter.getBeginTime()) && !StringUtils.isEmpty(queryParameter.getEndTime())){
            sqlBuffer.append(" and createTime >= ? and createTime <= ? ");
            parameter.add(queryParameter.getBeginTime());
            parameter.add(queryParameter.getEndTime());
        }

        try {
            return dbOperatorComponent.getBeanList(Notice.class,sqlBuffer.toString(),pagination,parameter.toArray());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return null;
        }
    }

    /**
     * 获取指定的通知
     *
     * @param id 通知id
     * @return 通知
     */
    public Notice get(String id){
        try {
            return dbOperatorComponent.getById(id,thisTableName,Notice.class);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 更新指定的通知
     *
     * @param notice notice
     */
    public void modify(Notice notice){
        List parameter = new ArrayList();
        StringBuffer sqlBuffer = new StringBuffer("update " + thisTableName + " set id = ? ");
        parameter.add(notice.getId());

        if(!StringUtils.isEmpty(notice.getContent())){
            sqlBuffer.append(" ,content = ? ");
            parameter.add(notice.getContent());
        }

        if(!StringUtils.isEmpty(notice.getTitle())){
            sqlBuffer.append(" ,title = ? ");
            parameter.add(notice.getTitle());
        }

        if(!StringUtils.isEmpty(notice.getAbstractContent())){
            sqlBuffer.append(" ,abstractContent = ? ");
            parameter.add(notice.getAbstractContent());
        }

        if(!StringUtils.isEmpty(notice.getType())){
            sqlBuffer.append(" ,type = ?");
            parameter.add(notice.getType());
        }

        sqlBuffer.append(" where id = ? ");
        parameter.add(notice.getId());

        try {
            dbOperatorComponent.update(sqlBuffer.toString(),parameter.toArray());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }
}
